<?php

// +----------------------------------------------------------------------
// | QSCMS
// +----------------------------------------------------------------------
// | Copyright (c)  2025 https://www.qqss.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed QSCMS is not free software, commercial use must purchase official license from official website to avoid unnecessary legal disputes.
// +----------------------------------------------------------------------
// | Author: Contract 990504246@qq.com
// +----------------------------------------------------------------------

namespace app\adminapi\controller\goods;

use app\adminapi\controller\Base;
use app\service\goods\SpecService;
use app\service\goods\GoodService;
use app\common\model\GoodsContentManager;


class Good extends Base
{

    /**
     * Goods list
     */
    public function list()
    {
        $where = $this->request->params([
            ['cate_id', ''],
            ['name', ''],
        ]);

        $service = new GoodService();
        $list    = $service->getGoodsList($where[1], $this->limit);

        $this->success('', [
            'list'  => $list->items(),
            'total' => $list->total(),
        ]);
    }

    /**
     * Get goods details
     */
    public function detail()
    {
        $id = inputs("id/d", 0);

        $service = new GoodService();
        $good    = $service->getGoodDetail($id);

        $this->success('', $good);
    }

    /**
     * Get goods data
     */
    private function postData(): array
    {
        return [
            "cate_id"           => inputs("cate_id/d", 0),
            "sort"              => inputs("sort/d", 0),
            "name"              => inputs("name/s", ""),
            "price"             => inputs("price/f", 0),
            "cost_price"        => inputs("cost_price/f", 0),
            "original_price"    => inputs("original_price/f", 0),
            "content"           => inputs("content/s", ""),
            "slider_image"      => inputs("slider_image/a", []),
            "paid_content_type" => inputs("paid_content_type/s", "text"),
            "spec_data"         => inputs("spec_data/a", []),
            "tags"              => inputs("tags/a", []),
            "views"             => inputs("views/d", 0),
            "sales_volume"      => inputs("sales_volume/d", 0),
            "comments_count"    => inputs("comments_count/d", 0),
            "seo_title"         => inputs("seo_title/s", ""),
            "seo_keywords"      => inputs("seo_keywords/s", ""),
            "seo_description"   => inputs("seo_description/s", ""),
            "template"          => inputs("template/s", ""),
        ];
    }

    /**
     * @notes Add goods
     * @auth true
     */
    public function add()
    {
        $post    = $this->postData();
        $service = new GoodService();
        $good    = $service->addGood($post);

        $this->success('', $good);
    }


    /**
     * @notes Edit goods
     * @auth true
     */
    public function edit()
    {
        $id      = inputs("id/d", 0);
        $post    = $this->postData();
        $service = new GoodService();
        $service->editGood($id, $post);

        $this->success();
    }

    /**
     * Get content type list
     */
    public function getContentTypeList()
    {
        $contentTypes = GoodsContentManager::getContentTypeList();
        $this->success('', $contentTypes);
    }

    /**
     * @notes Delete goods
     * @auth false
     */
    public function del()
    {
        $id = inputs("id/d", 0);

        $service = new GoodService();
        $service->deleteGood($id);

        $this->success();
    }

    public function status()
    {
        $id = inputs("id/d", 0);

        $service = new GoodService();
        $service->updateGoodStatus($id);

        $this->success();
    }


    /**
     * Filter goods by specification
     */
    public function filterBySpec()
    {
        // 获取基础筛选条件
        $where = $this->request->params([
            ['name', ''],
            ['cate_id', ''],
            ['status', ''],
        ]);

        // 获取规格筛选条件
        $spec_filters = inputs('spec_filters/a', []);

        $specService = new SpecService();
        $list        = $specService->filterGoodsBySpec($spec_filters, $where[1], $this->limit);

        $this->success('', [
            'list'  => $list->items(),
            'total' => $list->total(),
        ]);
    }

    /**
     * Get all filterable specification fields
     */
    public function getFilterFields()
    {
        $specService = new SpecService();
        $fields      = $specService->getAllFilterFields();

        $this->success('', $fields);
    }

    /**
     * Get specification by category
     */
    public function getCategorySpec()
    {
        $cate_id = inputs('cate_id/d', 0);
        if (empty($cate_id)) {
            $this->error(trans('category_id_required'));
        }

        $categoryService = new \app\service\goods\CategoryService();
        $spec            = $categoryService->getCategorySpec($cate_id);

        if (!$spec) {
            $this->success('', null);
            return;
        }

        $specService = new SpecService();
        $specData    = $specService->validateSpecForGood($spec->getData('id'));

        $this->success('', $specData);
    }

    /**
     * Get available template list for goods detail page
     */
    public function getTemplateList()
    {
        $service   = new GoodService();
        $templates = $service->getGoodsTemplateList();

        $this->success('', $templates);
    }
}
